import shopModel from '../../models/shopping/shop'
import {Food,Menu} from "../../models/shopping/food";
import ids from '../../prototype/ids'
import userinfo from "../../models/users/userInfo";
class Shop extends ids{
    constructor(){
        super()
        this.addShop = this.addShop.bind(this)
    }
    async addShop(req,res,next){
        try {
            console.log(req.query)
            let {name,address,phone,description,promotion_info,category,type1,image_path,license,float_delivery_fee,float_minimum_order_amount} = req.query
            // let rd = parseInt(Math.random()*7)
            // let rd1 = parseInt(Math.random()*7)
            // let city = ["山西","陕西","天津","南京","上海","北京","深圳"]
            // let date = ['111111111','111155444','455464256','22222131213','2142141241','22213123','2012321314']
            let no = await this.setID('shop_id');
            console.log(req.query.name,req.query.phone)
            // console.log(no)
            if(!image_path){
                throw '缺少店铺头像'
            }else if(!license){
                throw '缺少营业执照'
            }
            let shop = new shopModel({
                name,
                id:Number(no),
                phone,
                description,
                address,
                promotion_info,
                float_delivery_fee,
                float_minimum_order_amount,
                image_path,
                score:parseFloat(Math.random()*10,3),
                category,
                recent_order_num: parseInt(Math.random()*1000),
                'supports.description':type1,
                'license.business_license_image':license,
                'identification.licenses_date':new Date()
            })
            await shop.save()
            res.send({
                status:1,
                success:"添加成功"
            })
        }catch (e) {
            console.log(e)
            res.send({
                status:0,
                message:e,
                type:'ADD_SHOP_FAILED_PLACE_RE'
            })
        }

    }
    async getAllShop(req,res,next){
        let count = await shopModel.count({status:0});
        let {limit = 10,offset = 0} = req.query;
        let result = await shopModel.find({status:0},"-_id").sort({id:1}).limit(Number(limit)).skip(Number(offset));
        res.send({
            count,
            result
        })
    }
    async updateShop(req,res,next){
        // console.log(req.body)
        try {
            let result = await shopModel.findOne({
                id:req.body.id
            }).update({
                name:req.body["update[name]"],
                address:req.body["update[address]"],
                description:req.body["update[description]"],
                image_path:req.body.imageUrl,
                phone:req.body["update[phone]"],
                category:req.body["update[category][]"].join("/")
            });
            var a = await shopModel.findOne({
                id:req.body.id
            })
            // console.log(a)
            res.send({
                status:1,
                result
            })
        }catch (e) {
            res.send({
                status:0
            })
        }
    }
    async deleteShop(req,res,next){
        try{
            let result = await shopModel.findOneAndUpdate({
                id:req.query.id
            },{
                $set:{
                    status:1
                }
            })
            let result1 = await Food.updateMany({
                restaurant_id:req.query.id
            },{
                $set:{
                    is_featured:1
                }
            })
            let result2 = await Menu.find({
                restaurant_id:req.query.id
            })
            res.send({
                status:1,
                message:'删除成功'
            })
        }catch (e) {
            res.send({
                status:0,
                message:'数据异常'
            })
        }
    }
}
export default new Shop()